package com.dayuanit.dy8.mall.springbootmall.controller;

import com.dayuanit.dy8.mall.springbootmall.dto.ResponseDTO;
import com.dayuanit.dy8.mall.springbootmall.service.AddressService;
import com.dayuanit.dy8.mall.springbootmall.vo.AddressVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class AddressController {

    private static final Logger logger = LoggerFactory.getLogger(AddressController.class);

    @Autowired
    private AddressService addressService;

    @RequestMapping("/address/loadPro")
    public ResponseDTO loadPro() {
        return ResponseDTO.success(addressService.loadPro());
    }

    @RequestMapping("/address/loadCity")
    public ResponseDTO loadCity(String proCode) {
        return ResponseDTO.success(addressService.loadCity(proCode));
    }

    @RequestMapping("/address/loadArea")
    public ResponseDTO loadArea(String cityCode) {
        return ResponseDTO.success(addressService.loadArea(cityCode));
    }

    @RequestMapping("/address/save")
    public ResponseDTO save(@Validated AddressVO addressVO, BindingResult result) {

        final List<ObjectError> allErrors = result.getAllErrors();
        for (ObjectError error : allErrors) {
            return ResponseDTO.fail(error.getDefaultMessage());
        }

        logger.info(addressVO.getRealName());

        //sercie
        return ResponseDTO.success();
    }


}
